﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HtmlAgilityPack;
using Database;
namespace Example
{
    class Program
    {
        static void Main(string[] args)
        {
            //LayURLThongTinGheNgoi("https://www.galaxycine.vn/vi/thong-tin-phim/dai-nao-hoc-duong");
            var f = new Film();
        }

        public static List<DateTime> LayNgay(string khoichieu)
        {
            var d = new List<DateTime>();
            var bd = "";
            var kt = "";
            var str = "";
            foreach(var i in khoichieu)
            {
                if (i == '0' ||i == '1' ||i == '2' ||i == '3' ||i == '4' ||i == '5' ||i == '6' ||i == '7' ||i == '8' ||i == '9')
                {
                    str = str + i;
                }
            }
            bd = str.Substring(0, 8);
            kt = str.Substring(8, 8);
            var year = Int32.Parse(bd.Substring(4, 4));
            var month = Int32.Parse(bd.Substring(2, 2));
            var day = Int32.Parse(bd.Substring(0, 2));

            DateTime ngaybd = new DateTime(year,month,day);
            
            year = Int32.Parse(kt.Substring(4, 4));
            month = Int32.Parse(kt.Substring(2, 2));
            day = Int32.Parse(kt.Substring(0, 2));

            DateTime ngaykt = new DateTime(year, month, day);
            d.Add(ngaybd);
            d.Add(ngaykt);
            return d;
        }
        public static List<string> LayURL(int type)
        {
            var list = new List<string>();
            HtmlDocument ds = new HtmlDocument();
            HtmlWeb hw = new HtmlWeb();
            if (type == 0)//lay phim dang chieu
            {
                ds = hw.Load("https://www.galaxycine.vn/vi/phim/nowshowing");
            }
            else if (type == 1) // lay phim sap chieu
            {
                ds = hw.Load("https://www.galaxycine.vn/vi/phim/comingsoon");
            }
            var d = ds.DocumentNode.SelectNodes("//div[@class='rel']");
            
            foreach (HtmlNode link in d)
            {

                HtmlNodeCollection ls = link.ChildNodes;
                HtmlNode l = ls[1];
                var value = "";
                if (l.Attributes.Count > 0)
                    value = l.Attributes[0].Value;

                if (value.IndexOf("/vi/thong-tin-phim") == 0)
                {
                    list.Add("https://www.galaxycine.vn" + value);
                }
            }
            return list;
        }
        public static Film ThongTinFilm(string url)
        {
                HtmlDocument ds = new HtmlDocument();
                HtmlWeb hw = new HtmlWeb();                
                ds = hw.Load(url);
                
                var d = ds.DocumentNode.SelectNodes("//span[@class='content']");
                var film = new Film();
                film.Ten = (ds.DocumentNode.SelectSingleNode("//div[@class='title-article']")).InnerHtml;
                film.Khoichieu = d[0].InnerHtml;
                film.Theloai = d[1].InnerHtml;
                film.Phienban = d[5].InnerHtml;
                film.Nuocsanxuat = d[7].InnerHtml;
                film.Cumrap = d[8].InnerHtml;
             //   var dsd = (ds.DocumentNode.SelectSingleNode("//img[@class='img-movie-detail']"));
                film.Mota = (ds.DocumentNode.SelectSingleNode("//div[@class='content']")).ChildNodes[0].InnerHtml;
                film.Trailer = (ds.DocumentNode.SelectSingleNode("//input[@id='txtTrailerDetail']")).Attributes[2].Value;
                film.Url = url;
                film.Hinhanh = "https://www.galaxycine.vn" + (ds.DocumentNode.SelectSingleNode("//img[@class='img-movie-detail']")).Attributes[0].Value;

            return film;
        }

        public static List<Film> DsThongTinFilm(List<string> urllst)
        {
            var list = new List<Film>();
            foreach (var url in urllst)
            {
                var film = ThongTinFilm(url);
                list.Add(film);

            }

            return list;
        }

        public static List<string> LayURLThongTinGheNgoi(string url)
        {
            HtmlDocument ds = new HtmlDocument();
            HtmlWeb hw = new HtmlWeb();
            ds = hw.Load(url);

            var d = ds.DocumentNode.SelectNodes("//a[@class='mov_sc_hour btnShowtime']");
            var lst = new List<string>();
            foreach (var i in d)
            {
                lst.Add(i.Attributes[1].Value);
            }
            if (lst.Count > 0)
            {
                foreach (var str in lst)
                {
                    ds = hw.Load(str);
                    d = ds.DocumentNode.SelectNodes("//div[@class='book-info-content-item-content']");
                    var j = d[2].ChildNodes[1].InnerHtml;
                }
            }
            return lst;
        }
        
    }
        public class Film
        {
            private string _ten = "";

            public string Ten
            {
                get { return _ten; }
                set { _ten = value; }
            }
            private string _khoichieu = "";

            public string Khoichieu
            {
                get { return _khoichieu; }
                set { _khoichieu = value; }
            }
            private string _theloai = "";

            public string Theloai
            {
                get { return _theloai; }
                set { _theloai = value; }
            }
            private string _phienban = "";

            public string Phienban
            {
                get { return _phienban; }
                set { _phienban = value; }
            }
            private string _nuocsanxuat = "";

            public string Nuocsanxuat
            {
                get { return _nuocsanxuat; }
                set { _nuocsanxuat = value; }
            }
            private string _cumrap = "";

            public string Cumrap
            {
                get { return _cumrap; }
                set { _cumrap = value; }
            }
            private string _mota = "";

            public string Mota
            {
                get { return _mota; }
                set { _mota = value; }
            }
            private string _trailer = "";

            public string Trailer
            {
                get { return _trailer; }
                set { _trailer = value; }
            }

            private string _url = "";

            public string Url
            {
                get { return _url; }
                set { _url = value; }
            }
            private string _hinhanh = "";

            public string Hinhanh
            {
                get { return _hinhanh; }
                set { _hinhanh = value; }
            }
            public Film()
            {

            }

        
    }
}
